/************************************************************/
/*文档名称：staffing_sys_client.c                           */
/*文档作者：                                                */
/*创建时间：2017.                                           */
/*文档版本：v                                               */
/*文档简介：                                                */
/*文档说明：                                                */
/************************************************************/
#include "staffing_sys.h"

int sockfd;
int cmd;
int menu_flag = 0;
int i;
struct sockaddr_in serveraddr, clientaddr;
transmsg rc_msg, sr_msg;
socklen_t addrlen = sizeof(struct sockaddr_in);

void do_login()
{
	//input username and password,both of them can't be empty.
	printf("Input username:");
	fgets(sr_msg.user.name,128,stdin);
	if(sr_msg.user.name[0] == 0)
	{
		printf("username can't be empty!\n");
	}
	sr_msg.user.name[strlen(sr_msg.user.name) - 1] = 0;

	printf("Input password:");
	fgets(sr_msg.user.password,128,stdin);
	if(sr_msg.user.password[0] == 0)
	{
		printf("password can't be empty!\n");
	}
	sr_msg.user.password[strlen(sr_msg.user.password) - 1] = 0;

	//login   : if username is not exit ,wrong.
	if(send(sockfd,&sr_msg,sizeof(transmsg),0) < 0)
	{
		err_log("Failed to send");
	}
	memset(&sr_msg,0,sizeof(transmsg));
	if(recv(sockfd,&sr_msg,sizeof(transmsg),0) < 0)
	{
		err_log("Failed to recv");
	}
	if(sr_msg.cmd == DO_LOGIN)
		menu_flag = 2;             //leave out the first level menu.
	else if(sr_msg.cmd == DO_LOGIN|DO_FAILED)
		printf("username or password is wrong!\n");
}

void do_exit()
{
	sr_msg.cmd = DO_EXIT|menu_flag;
	menu_flag -- ;
	if(send(sockfd,&sr_msg,sizeof(transmsg),0) < 0)
	{
		err_log("Failed to send");
	}
	
}

/************************************************/
/* name     : do_delete                         */
/* parameter: cmd no, user type                 */
/* funciton : delete user from table            */
/* author   : Raymond                           */
/************************************************/
void do_delete(unsigned int cmd, int type)
{
	transmsg new_msg;

	if(NORMAL == type){
		printf("permission denied\n");
		exit(0);
	}

	bzero(&new_msg, sizeof(transmsg));
	printf("input no:");
	scanf("%d",&new_msg.info.no);

	new_msg.cmd = cmd;
	new_msg.user.type = type;

	if(send(sockfd, &new_msg, sizeof(transmsg), 0) < 0){
		err_log("Fail to send msg");
	}

	bzero(&new_msg, sizeof(transmsg));
	if(recv(sockfd, &new_msg, sizeof(transmsg), 0) < 0){
		err_log("recv error");
	}else{
		if(new_msg.cmd == (cmd | DO_ABORD)){
			printf("permission denied or user not exist\n");
		}else if(new_msg.cmd == (cmd | DO_FAILED)){
			printf("delete user failed\n");
		}else{
			printf("delete done\n");
		}
	}

}

/************************************************/
/* name     : do_adduser                        */
/* parameter: user type                         */
/* funciton : get user info from client         */
/* author   : Raymond                           */
/************************************************/
int do_adduser(unsigned int cmd, int type)
{
	transmsg new_msg;

	bzero(&new_msg, sizeof(transmsg));
	
	new_msg.cmd = cmd;
	new_msg.user.type = type;

	printf("input user name:");
	scanf("%s",new_msg.user.name);
	strcpy(new_msg.info.name,new_msg.user.name);

	printf("input user password:");
	scanf("%s",new_msg.user.password);

	printf("input user id:");
	scanf("%d",&new_msg.info.no);

	printf("input user salary:");
	scanf("%lf",&new_msg.info.salary);

	if(send(sockfd, &new_msg, sizeof(transmsg), 0) < 0){
		err_log("Fail to send msg");
	}

	bzero(&new_msg, sizeof(transmsg));
	if(recv(sockfd, &new_msg, sizeof(transmsg), 0) < 0){
		err_log("recv error");
	}
	if(new_msg.cmd == (cmd | DO_EXIST)){
		printf("user is exist\n");
	}else if(new_msg.cmd == (cmd | DO_FAILED)){
		printf("insert failed\n");
	}else{
		printf("insert done\n");
	}

	return 0;
}


/*******************************************************
 main() 
 *******************************************************/
int main(int argc, const char *argv[])
{

	if(argc < 3)
	{
		fprintf(stderr,"Usage:%s serverip port.\n",argv[0]);
		return -1;
	}

	//socket
	if(-1 == (sockfd = socket(AF_INET,SOCK_STREAM,0)))
	{
		err_log("Failed to socket");
	}

	//Fill the addr
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_addr.s_addr =inet_addr(argv[1]);
	serveraddr.sin_port =htons(atoi(argv[2]));

	//Connected to server.
	if(-1 == connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)))
	{
		err_log("Fail to connect");
	}
	menu_flag = 1;
	//主循环
	printf("Welcome to use Staffing system. v0.0\n");
	
	while(menu_flag != 0)
	{
		//第一级菜单
		//只有登录成功才会跳出第一级目录
		while(menu_flag == 1)
		{
			printf("**********************************************\n");
			printf("**       1:login         2:quit             **\n");
			printf("**********************************************\n");
			printf("Please input cmd: ");
			
			//Input check
			if(scanf("%d",&cmd) == 0)
			{
				puts("Input Invalid.");
				while(getchar() != '\n');
				continue;
			}
			while(getchar() != '\n');
			if(cmd < 3 && cmd >0)
			{
				sr_msg.cmd = cmd << 28;
			}
			else
			{
				printf("Input Invalid!\n");
				continue;
			}
			if(sr_msg.cmd == DO_EXIT)
			{
				do_exit();
			}
			else if(sr_msg.cmd == DO_LOGIN)
			{
				do_login();
			}
		}
		//二级菜单
		while(menu_flag == 2)
		{
			int i = 0;
			printf("**********************************************\n");
			if(sr_msg.user.type == ROOT)
				printf("**%d:add admin                               **\n",++i);
			if(sr_msg.user.type < NORMAL)
			{
				printf("**%d:add user                                **\n",++i);
				printf("**%d:delete user                             **\n",++i);
			}
			printf("**%d:query                                  **\n",++i);
			printf("**%d:update                                 **\n",++i);
			printf("**%d:quit                                   **\n",++i);
			printf("**********************************************\n");
			printf("Please input cmd: ");

			if(scanf("%d",&cmd) == 0)
			{
				puts("Input Invalid.");
				while(getchar() != '\n');
				continue;
			}
			while(getchar() != '\n');
			if(cmd <7 && cmd >0)
			{
				sr_msg.cmd = (cmd + sr_msg.user.type+2) << 28;
			}
			else
			{
				printf("Input Invalid!\n");
				continue;
			}
			
			switch(sr_msg.cmd)
			{
				case DO_LOGIN:
					do_login();
					break;
				case 0x80000000:
				case DO_EXIT:
					do_exit();
					break;
				case DO_ADDADMIN:
					do_adduser(sr_msg.cmd, ADMIN);
					break;
				case DO_ADDUSER:
					do_adduser(sr_msg.cmd, NORMAL);
					break;
				case DO_DELETE:
					do_delete(sr_msg.cmd, sr_msg.user.type);
				default :
					break;
			}
		}
	}
	close(sockfd);
	return 0;
}
